﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxCallback;
using System.Web.Security;
using System.Configuration;

namespace TireService.Web.UserControls
{
    public partial class ucCustomerBookingLink : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void callbackSendCustomerBookingLinkPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int CustomerId)
        {
            // Show usercontrol edit form
            pSendCustomerBookingLink.Visible = true;

            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            if (CustomerId != 0)
            {
                LoadCustomerBookingLink(CustomerId, db);
            }
            else
            {
                // Error
                throw new ArgumentException("Link til booking kunne ikke genereres - kan skyldes at kunde ikke har et brugerlogin.");
            }
        }

        /// <summary>
        /// Function that load customer booking link
        /// </summary>
        /// <param name="CustomerId">Id on Customer</param>
        /// <param name="db">Connection to database</param>
        private void LoadCustomerBookingLink(int CustomerId, TireServiceDB db)
        {
            // Fetch Customer
            Customer customer = db.GetCustomer(CustomerId);

            if (customer == null)
            {
                // Error
                throw new ArgumentException("Kunde blev ikke fundet");
            }

            // Set values in edit form
            if (customer.UserId != null)
            {
                lblBookingLinkValue.Text = string.Format(ConfigurationManager.AppSettings["Website"] + "Booking/Booking.aspx?uid={0}", customer.UserId.ToString());
                hlBookingLink.NavigateUrl = string.Format(ConfigurationManager.AppSettings["Website"] + "Booking/Booking.aspx?uid={0}", customer.UserId.ToString());
                hlBookingLink.Text = "Bookinglink - test";
            }
            else
            {
                // Error
                throw new ArgumentException("Kunde har ikke et brugerlogin - venligst opret dette først.");
            }
        }

        /// <summary>
        /// Callback function that send email to customer with booking link
        /// </summary>
        protected void callBackSendCustomerBookingLink_Callback(object sender, CallbackEventArgs e)
        {
            try
            {
                int CustomerId = Convert.ToInt32(e.Parameter);

                TireServiceDB db = new TireServiceDB();

                Customer customer = db.GetCustomer(CustomerId);

                if (customer != null)
                {
                    if (customer.UserId != null)
                    {
                        string BookingLink = string.Format(ConfigurationManager.AppSettings["Website"] + "Booking/Booking.aspx?uid={0}", customer.UserId.ToString());

                        EmailNotificationServices emailService = new EmailNotificationServices();
                        emailService.CustomerBookingLink_SendCustomerEmail(customer.Email, customer.Name, CustomerId, Membership.GetUser(), BookingLink); 
                    }
                    else
                    {
                        // Error
                        throw new ArgumentException("Kunde har ikke et brugerlogin - venligst opret dette først.");
                    }
                }
                else
                {
                    // Error
                    throw new ArgumentException("Kunde blev ikke fundet");
                }

                // Dispose database connection
                db.Dispose();
            }
            catch (Exception ex)
            {
                // Error
                throw new ArgumentException("Fejl opstod: " + ex.Message);
            }
        }
    }
}